﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
namespace SoftWareBoot
{

    /// <summary>
    /// 文件升级信息结构
    /// </summary>
    internal class FileUpdateInfo : DependencyObject
    {

        public static DependencyProperty FilePathProperty = DependencyProperty.Register("FilePath", typeof(string), typeof(FileUpdateInfo));
        public static DependencyProperty FileStatePropery = DependencyProperty.Register("FileState", typeof(string), typeof(FileUpdateInfo));


        public string FilePath
        {
            get
            {
                return (string)GetValue(FilePathProperty);
            }
            set
            {
                SetValue(FilePathProperty, value);
            }
        }

        public string FileState
        {
            get
            {
                return (string)GetValue(FileStatePropery);
            }
            set
            {
                SetValue(FileStatePropery, value);
            }
        }

        private FileUpdateInfo()
        {

        }

        public static FileUpdateInfo GetFileUpdateInfo(AutoUpdateService.FileDataInfo fileInfo)
        {
            string path = string.Format(@"{0}\Bin\{1}", System.Environment.CurrentDirectory, fileInfo.Name);

            if (System.IO.File.Exists(path))
            {
                if (fileInfo.MD5 == getFileMD5(path)) return null;

            }

            FileUpdateInfo info = new FileUpdateInfo() { FileState = "", FilePath = fileInfo.Name };

            return info;
        }

        /// <summary>
        /// 获取文件MD5效验码
        /// </summary>
        /// <param name="filepath"></param>
        /// <returns></returns>
        private static string getFileMD5(string filepath)
        {
            System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();

            string md5CheckCode = "";

            using (System.IO.FileStream fs = new System.IO.FileStream(filepath, System.IO.FileMode.Open))
            {
                md5CheckCode = BitConverter.ToString(md5.ComputeHash(fs)).Replace("-", "");
            }
            return md5CheckCode;
        }
    }
}
